using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Terminal.Advanced_AEUC_settings_controls.Settings_Interfaces;
using Terminal.Advanced_AEUC_settings_controls.Settings_AUX;
using Auxiliaries;

namespace Terminal.Advanced_AEUC_settings_controls.Settings_Editing_Forms
{
    /// <summary>
    /// Form for editing advanced AEUC controls settings
    /// </summary>
    public partial class AEUCSettingEditForm : Form
    {
        IAEUCSetting editSetting;
        /// <summary>
        /// Public contructor taking an AEUC control reference
        /// </summary>
        /// <param name="ieditSetting">Reference to the AEUC control for editing</param>
        public AEUCSettingEditForm(IAEUCSetting ieditSetting)
        {
            editSetting = ieditSetting;

            InitializeComponent();

            #region Get the current AEUC settings of the control
            tbID.Text = tbCurID.Text = editSetting.AEUC_Id.ToString();
            tbSize.Text = tbCurSize.Text = editSetting.AEUC_Size.ToString();
            tbVal.Text = tbCurVal.Text = editSetting.AEUC_Val.ToString();
            tbOptVal.Text = tbCurOptVal.Text = editSetting.AEUC_OptVal.ToString();
            tbDefVal.Text = tbCurDefVal.Text = editSetting.AEUC_Default.ToString();
            tbMinVal.Text = tbCurMinVal.Text = editSetting.AEUC_Min.ToString();
            tbMaxVal.Text = tbCurMaxVal.Text = editSetting.AEUC_Max.ToString();
            chbSettingEnabled.Checked = editSetting.AEUC_Enabled;
            chbPointerField.Checked = editSetting.AEUC_RefPointer;
            chbStateSense.Checked = editSetting.AEUC_SupportStateSense;
            tbDisplayName.Text = editSetting.AEUC_DisplayName;
            #endregion

            #region Check if this is an enumeration type control and update UI
            if (editSetting is IAEUCEnumeration)
            {
                lblCurVal.Visible = tbCurVal.Visible = false;
                lblCurOptVal.Visible = tbCurOptVal.Visible = false;
                lblVal.Visible = tbVal.Visible = false;
                lblOptVal.Visible = tbOptVal.Visible = false;
                btnEditEnumItems.Visible = true;
            }
            #endregion
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            bool faulty_input = false;

            uint id;
            uint size;
            Int64 val;
            Int64 opt_val;
            Int64 def_val;
            Int64 min_val;
            Int64 max_val;

            if (!uint.TryParse(tbID.Text, out id))
            {
                UInt64 tmpVal;
                faulty_input = !HEX_converter.ToBinary(tbID.Text, out tmpVal);
                if (faulty_input)
                    MessageBox.Show("Please type a correct id value.");
                else
                    id = (uint)tmpVal;
            }
            if (!uint.TryParse(tbSize.Text, out size))
            {
                UInt64 tmpVal;
                faulty_input = !HEX_converter.ToBinary(tbSize.Text, out tmpVal);
                if (faulty_input)
                    MessageBox.Show("Please type a correct size value.");
                else
                    size = (uint)tmpVal;
            }
            if (!Int64.TryParse(tbVal.Text, out val))
            {
                UInt64 tmpVal;
                faulty_input = !HEX_converter.ToBinary(tbVal.Text, out tmpVal);
                if (faulty_input)
                    MessageBox.Show("Please type a correct size value.");
                else
                    val = (Int64)tmpVal;
            }
            if (!Int64.TryParse(tbOptVal.Text, out opt_val))
            {
                UInt64 tmpVal;
                faulty_input = !HEX_converter.ToBinary(tbOptVal.Text, out tmpVal);
                if (faulty_input)
                    MessageBox.Show("Please type a correct size value.");
                else
                    opt_val = (Int64)tmpVal;
            }
            if (!Int64.TryParse(tbDefVal.Text, out def_val))
            {
                UInt64 tmpVal;
                faulty_input = !HEX_converter.ToBinary(tbDefVal.Text, out tmpVal);
                if (faulty_input)
                    MessageBox.Show("Please type a correct size value.");
                else
                    def_val = (Int64)tmpVal;
            }
            if (!Int64.TryParse(tbMinVal.Text, out min_val))
            {
                UInt64 tmpVal;
                faulty_input = !HEX_converter.ToBinary(tbMinVal.Text, out tmpVal);
                if (faulty_input)
                    MessageBox.Show("Please type a correct size value.");
                else
                    min_val = (Int64)tmpVal;
            }
            if (!Int64.TryParse(tbMaxVal.Text, out max_val))
            {
                UInt64 tmpVal;
                faulty_input = !HEX_converter.ToBinary(tbMaxVal.Text, out tmpVal);
                if (faulty_input)
                    MessageBox.Show("Please type a correct size value.");
                else
                    max_val = (Int64)tmpVal;
            }

            if (!faulty_input)
            {
                editSetting.AEUC_Id = id;
                editSetting.AEUC_Size = size;
                editSetting.AEUC_Val = val;
                editSetting.AEUC_OptVal = opt_val;
                editSetting.AEUC_Default = def_val;
                editSetting.AEUC_Min = min_val;
                editSetting.AEUC_Max = max_val;
                editSetting.AEUC_Enabled = chbSettingEnabled.Checked;
                editSetting.AEUC_RefPointer = chbPointerField.Checked;
                editSetting.AEUC_SupportStateSense = chbStateSense.Checked;
                editSetting.AEUC_DisplayName = tbDisplayName.Text;
            }

            Close();
        }

        private void btnEditEnumItems_Click(object sender, EventArgs e)
        {
            AEUCEnumerationEditForm AEUCenumForm = new AEUCEnumerationEditForm((IAEUCEnumeration)editSetting);
            AEUCenumForm.ShowDialog();
        }
    }
}